﻿using System.Configuration;
using System.Data;

using contSys.database.DAL;
using contSys.Security;
using System.Diagnostics;
using System;

namespace contSys.BL.Simulator
{
    public class Moves
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "Moves";

        public static string LastErrorMsg = string.Empty;

        public static DataSet GetMovesByUnit(string usr, string pwd, object unitID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM vwMovesWithTypeDescr WHERE UnitID = {0}", unitID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        private static bool checkMoveValidity(object moveType)
        {
            int mt;
            int recCount;

            if (int.TryParse(moveType.ToString(), out mt))
            {
                string sql = string.Format("SELECT * FROM lutMoveTypes WHERE (ID={0})", mt);
                if (SQLiteDB.TryCountRecords(_connString, sql, out recCount))
                {
                    if (recCount > 0) return true;
                }
            }

            LastErrorMsg = "invalid MoveType";
            return false;
        }

        public static bool ModifyMove(string usr, string pwd, object unitID, object moveID, object moveType)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                if (checkMoveValidity(moveType))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("UPDATE Moves SET moveType={0} WHERE (UnitID={1} AND MoveID={2})", moveType.ToString(), unitID.ToString(), moveID.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool AddMove(string usr, string pwd, object unitID, object moveID, object moveType)
        {
            if (UserManager.AddAllowed(tableObjName, usr, pwd))
            {
                if (checkMoveValidity(moveType))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("INSERT INTO Moves (UnitID, moveID, moveType) VALUES ({0},{1},{2})", unitID.ToString(), moveID.ToString(), moveType.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool DeleteMove(string usr, string pwd, object unitID, object moveID)
        {
            if (UserManager.DeleteAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                return SQLiteDB.TryExecuteScalar(_connString, String.Format("DELETE FROM Moves WHERE (UnitID={0} AND MoveID={1})", unitID.ToString(),  moveID.ToString()));
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        private static void Writelog(string msg)
        {
            const string cs = "contSys.BL.Simulator";

            EventLog elog = new EventLog();
            if (!EventLog.SourceExists(cs))
                EventLog.CreateEventSource(cs, cs);

            elog.Source = cs;
            elog.EnableRaisingEvents = true;
            elog.WriteEntry(msg);
        }
    }
}
